#include "createnodedialog.h"
#include "nodemanager/wnodemanager.h"

CreateNodeDialog::CreateNodeDialog(wxWindow* parent, const wxString& title)
    : wxDialog(parent, wxID_ANY, title, wxDefaultPosition, wxSize(300, 150))
{
    // 创建主垂直布局
    wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);

    // 创建标签和输入框的水平布局
    wxBoxSizer* inputSizer = new wxBoxSizer(wxHORIZONTAL);

    // 创建标签
    wxStaticText* label = new wxStaticText(this, wxID_ANY, wxT("结点名称:"));
    inputSizer->Add(label, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 10);

    // 创建输入框
    m_textCtrl = new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxSize(150, -1));
    inputSizer->Add(m_textCtrl, 1, wxALIGN_CENTER_VERTICAL);

    // 将输入布局添加到主布局
    mainSizer->Add(inputSizer, 0, wxEXPAND | wxALL, 15);

    // 创建按钮
    wxButton* createButton = new wxButton(this, wxID_ANY, wxT("确定创建"));
    createButton->Bind(wxEVT_BUTTON, &CreateNodeDialog::OnCreateButtonClicked, this);

    // 将按钮添加到主布局
    mainSizer->Add(createButton, 0, wxALIGN_CENTER | wxLEFT | wxRIGHT | wxBOTTOM, 15);

    // 设置对话框的主布局
    SetSizer(mainSizer);

    // 调整窗口大小以适应内容
    Layout();
    Fit();
    this->CenterOnScreen();
}

void CreateNodeDialog::OnCreateButtonClicked(wxCommandEvent& event)
{
    // 验证输入
    if (m_textCtrl->GetValue().IsEmpty())
    {
        wxMessageBox(wxT("请输入结点名称"), wxT("错误"), wxOK | wxICON_ERROR, this);
        return;
    }

    // 如果输入有效，关闭对话框并返回wxID_OK
    EndModal(wxID_OK);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

CreateRouteConditionDialog::CreateRouteConditionDialog(wxWindow* parent, const wxString& title)
    : wxDialog(parent, wxID_ANY, title, wxDefaultPosition, wxSize(300, 150))
{
    // 创建主垂直布局
    wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);

    // 创建标签和输入框的水平布局
    wxBoxSizer* inputSizer = new wxBoxSizer(wxHORIZONTAL);

    wxArrayString nodeArray;
    std::map<wxString,WBaseNode*> allnodes = WNodeManager::getSingleton().getAllNodes();
    std::map<wxString,WBaseNode*>::iterator iternode = allnodes.begin();
    for(;iternode != allnodes.end();++iternode)
    {
        if((*iternode).second->getNodeType() == NodeType::NODE_SIGNAL)
            nodeArray.push_back((*iternode).second->getName());
    }

    // 创建标签
    wxStaticText* label = new wxStaticText(this, wxID_ANY, wxT("开始信号灯:"));
    inputSizer->Add(label, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 10);

    // 创建输入框
    m_startLightCtrl = new wxComboBox(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
                                      nodeArray, wxCB_READONLY);
    inputSizer->Add(m_startLightCtrl, 1, wxALIGN_CENTER_VERTICAL);
    m_startLightCtrl->SetSelection(0);

    wxBoxSizer* inputSizer2 = new wxBoxSizer(wxHORIZONTAL);

    // 创建标签
    label = new wxStaticText(this, wxID_ANY, wxT("结束信号灯:"));
    inputSizer2->Add(label, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 10);

    // 创建输入框
    m_endLightCtrl = new wxComboBox(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
                                    nodeArray, wxCB_READONLY);
    inputSizer2->Add(m_endLightCtrl, 1, wxALIGN_CENTER_VERTICAL);
    m_endLightCtrl->SetSelection(0);

    // 将输入布局添加到主布局
    mainSizer->Add(inputSizer, 0, wxEXPAND | wxALL, 15);
    mainSizer->Add(inputSizer2, 0, wxEXPAND | wxALL, 15);

    // 创建按钮
    wxButton* createButton = new wxButton(this, wxID_ANY, wxT("确定创建"));
    createButton->Bind(wxEVT_BUTTON, &CreateRouteConditionDialog::OnCreateButtonClicked, this);

    // 将按钮添加到主布局
    mainSizer->Add(createButton, 0, wxALIGN_CENTER | wxLEFT | wxRIGHT | wxBOTTOM, 15);

    // 设置对话框的主布局
    SetSizer(mainSizer);

    // 调整窗口大小以适应内容
    Layout();
    Fit();
    this->CenterOnScreen();
}

void CreateRouteConditionDialog::OnCreateButtonClicked(wxCommandEvent& event)
{
    // 验证输入
    if (m_startLightCtrl->GetValue().IsEmpty())
    {
        wxMessageBox(wxT("请输入开始信号灯名称"), wxT("错误"), wxOK | wxICON_ERROR, this);
        return;
    }

    if (m_endLightCtrl->GetValue().IsEmpty())
    {
        wxMessageBox(wxT("请输入结束信号灯名称"), wxT("错误"), wxOK | wxICON_ERROR, this);
        return;
    }

    if(m_startLightCtrl->GetValue() == m_endLightCtrl->GetValue())
    {
        wxMessageBox(wxT("开始信号灯和结束信号灯名称不能相同."), wxT("错误"), wxOK | wxICON_ERROR, this);
        return;
    }

    // 如果输入有效，关闭对话框并返回wxID_OK
    EndModal(wxID_OK);
}
